//
//  ImageLoaderUtility.m
//  SinaWeather
//
//  Created by nan4 li on 12-6-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ImageLoaderUtility.h"
@implementation UIButton(ImageCache)
- (void)setButtonImageWithUrl:(NSString*)url placeholderImage:(UIImage *)placeholder
{
    [self retain];//仿制uibutton被释放delegate崩溃问题
    [self setImage:placeholder forState:UIControlStateNormal];
    ImageDLoader *downloader = [[ImageDLoader alloc] init];
    [downloader getImageFromURL:url delegate:self];
    [downloader release];
}

#pragma -ImageDLoaderDelegate
- (void)receivedImage:(NSData*)imageData sender:(ImageDLoader*)sender
{
    if (imageData.length>0) {
        [self setImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
    }
    [self release];
}

@end

@implementation UIImageView(ImageCache)
- (void)setImageViewWithUrl:(NSString *)url placeholderImage:(UIImage *)placeholder
{
    [self retain];
    [self setImage:placeholder];
    ImageDLoader *downloader = [[ImageDLoader alloc] init];
    [downloader getImageFromURL:url delegate:self];
    [downloader release];
}

#pragma -ImageDLoaderDelegate
- (void)receivedImage:(NSData*)imageData sender:(ImageDLoader*)sender
{
    if (imageData.length>0) {
        [self setImage:[UIImage imageWithData:imageData]];
    }
    [self release];
}

@end
